package com.server.tools;
/**
* @author	李小布
* @email	397554852@qq.com
* @version	创建时间：2018年9月26日 上午10:35:06
*/

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.server.ServerUtils;
import com.server.background.data.App;
import com.server.background.data.AppSwitch;
import com.server.background.data.RankData;
import com.server.background.data.RankTableData;
import com.server.db.DataBaseManager;
import com.server.db.QueryData;
import com.server.tools.timertask.LmyScheduledTask;

public class AppData {
	public static Map<Long, App> appMap = new HashMap<Long, App>();
	public static Map<Long, AppSwitch> appSwitchMap = new HashMap<Long, AppSwitch>();
	public static Map<Integer, RankTableData> rankTableMap = new HashMap<Integer, RankTableData>();
	public static Map<Integer, List<RankData>> rankDataMap = new HashMap<Integer, List<RankData>>();
	
	public static void InitAppData() {
		DataBaseManager.GetInstance().InitAppMap();
    	//System.out.println(JsonUtil.ObjectToJsonString(appMap));
		DataBaseManager.GetInstance().InitAppSwitchMap();
    	//System.out.println(JsonUtil.ObjectToJsonString(appSwitchMap));
		DataBaseManager.GetInstance().InitRanklistTable();
		ServerUtils.store_map = DataBaseManager.GetInstance().GetStoreData();
		InitRanklist();
    	//System.out.println(JsonUtil.ObjectToJsonString(rankTableMap));
    	
    	//开启日计划任务
		LmyScheduledTask DayPlay = new LmyScheduledTask(null,"DayPlan",0,0,5,0);
		DayPlay.Start();
		
		//开启小时计划任务
    	Calendar c = Calendar.getInstance();
		int hour = c.get(Calendar.HOUR_OF_DAY);
		LmyScheduledTask HourPlay = new LmyScheduledTask("com.server.tools.timertask.TimerTaskFunction", "HourPlan",hour,0,0,60*60*1000);
		HourPlay.Start();
	}
	
	/**
	 * 获取全部app开关列表
	 * @return
	 */
	public static String SwitchToArray() {
		List<AppSwitch> list = new ArrayList<AppSwitch>(appSwitchMap.values());
		return JsonUtil.ObjectToJsonString(list);
	}
	
	/**
	 * 初始化排行榜
	 */
	public static void InitRanklist() {
		for(RankTableData rk : AppData.rankTableMap.values()) {
			DataBaseManager.GetInstance().GetRanklist(rk.rkid, rk.rkname);
		}
	}
	
	/**
	 * 根据权限获取app的开关列表
	 * @param power_list
	 * @return
	 */
	public static String SwitchToArray(List<Long> power_list) {
		List<AppSwitch> list = new ArrayList<AppSwitch>();
		for(int i=0;i<power_list.size();i++) {
			if(appSwitchMap.containsKey(power_list.get(i))) {
				list.add(appSwitchMap.get(power_list.get(i)));	
			}
		}
		return JsonUtil.ObjectToJsonString(list);
	}
	
	/**
	 * 获取全部app列表
	 * @return
	 */
	public static String GetAppList() {
		List<App> list = new ArrayList<App>(appMap.values());
		return JsonUtil.ObjectToJsonString(list);
	}
	
	/**
	 * 根据权限获取app列表
	 * @param power_list
	 * @return
	 */
	public static List<App> GetPowerAppList(List<Long> power_list) {
		List<App> appList = new ArrayList<App>();
		for(int i=0;i<power_list.size();i++) {
			if(appMap.containsKey(power_list.get(i))) {
				appList.add(appMap.get(power_list.get(i)));	
			}
		}
    	return appList;
	}
	
	/**
	 * 检测排行榜表是否存在
	 * @param rkname
	 * @return
	 */
	public static boolean JudgeRankTableContains(String rkname) {
		boolean bool = false;
		for (RankTableData data : rankTableMap.values()) {
			bool = data.rkname.equals(rkname);
		}
		return bool;
	}
	
	public static List<RankData> ClientGetRankList(String rkname){
		for (RankTableData data : rankTableMap.values()) {
			if(data.rkname.equals(rkname)) {
				return rankDataMap.get(data.rkid);
			}
		}
		return new ArrayList<RankData>();
	}
	
	/**
	 * 获取排行榜列表
	 * @param rkname
	 * @return
	 */
	public static List<RankTableData> GetAllRanklist(int uid, int power) {
		if(power >= 9) {
			return new ArrayList<RankTableData>(rankTableMap.values());
		}else {
			List<RankTableData> list = new ArrayList<RankTableData>();
			try {
				List<Long> power_list = QueryData.GetInstance().GetPowerList(uid);
				for (int i=0;i<power_list.size();i++) {
					for (RankTableData value : rankTableMap.values()) {
						if(value.appid == power_list.get(i)) {
							list.add(value);
						}
					}
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return list;
		}
	}
}
